import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yanli on 16/7/28.
 */
public class Data {

    public static final Data me = new Data();

    //根据时间统计所有广告数据
    public static List<JSONObject> fbTimeSumStatistic = new ArrayList<>();
    //根据时间统计banner数据
    public static List<JSONObject> fbTimeBannerStatistic = new ArrayList<>();
    //根据时间统计插屏广告数据
    public static List<JSONObject> fbTimeFullscreenStatistic = new ArrayList<>();
    //根据时间统计原生广告数据
    public static List<JSONObject> fbTimeNativeStatistic = new ArrayList<>();
    //根据国家统计banner广告数据
    public static List<JSONObject> fbCountryBannerStatistic = new ArrayList<>();
    //根据国家统计插屏广告数据
    public static List<JSONObject> fbCountryFullscreenStatistic = new ArrayList<>();
    //根据国家同价原生广告数据
    public static List<JSONObject> fbCountryNativeStatistic = new ArrayList<>();


    public static String Banner = "";
    public static String FullScreen = "";
    public static String NativeAd = "";

    public static void initAdType(String banner, String fullScreen, String Native){
        if (StringUtils.isNotBlank(banner)){
            Banner = banner;
        } if (StringUtils.isNotBlank(fullScreen)){
            FullScreen = fullScreen;
        } if (StringUtils.isNotBlank(Native)){
            NativeAd = Native;
        }
    }
    //按国家统计数据
    public void setCountryDate(JSONObject dataOfRequest1, String type) {

        List<JSONObject> data = JSONObject.parseArray(dataOfRequest1.getJSONArray("data").toJSONString(), JSONObject.class);

        for (JSONObject json : data){
            String adType = json.getJSONObject("breakdowns").getString("placement");
            if (adType.equals(Banner)){
                manageCountryDate(fbCountryBannerStatistic, json, type);
            }else if (adType.equals(FullScreen)){
                manageCountryDate(fbCountryFullscreenStatistic, json, type);
            }else if (adType.equals(NativeAd)){
                manageCountryDate(fbCountryNativeStatistic, json, type);
            }
        }
    }

    //按时间统计数据
    public static void setTimeDate(JSONObject dataOfRequest1, String type){
        List<JSONObject> data = JSONObject.parseArray(dataOfRequest1.getJSONArray("data").toJSONString(), JSONObject.class);

        //初始化时间段的统计信息
        if (fbTimeBannerStatistic.size() == 0){
            JSONObject jsonObject;
            for (String date : Common.dates){
                jsonObject = new JSONObject();
                jsonObject.put("time", date);
                jsonObject.put(type, 0.0);
                fbTimeBannerStatistic.add(jsonObject);
                jsonObject = new JSONObject();
                jsonObject.put("time", date);
                jsonObject.put(type, 0.0);
                fbTimeFullscreenStatistic.add(jsonObject);
                jsonObject = new JSONObject();
                jsonObject.put("time", date);
                jsonObject.put(type, 0.0);
                fbTimeNativeStatistic.add(jsonObject);
                jsonObject = new JSONObject();
                jsonObject.put("time", date);
                jsonObject.put(type, 0.0);
                fbTimeSumStatistic.add(jsonObject);
            }
        }else {
            for (JSONObject statistic : fbTimeSumStatistic){
                statistic.put(type, 0.0);
            }
            for (JSONObject statistic : fbTimeBannerStatistic){
                statistic.put(type, 0.0);
            }
            for (JSONObject statistic : fbTimeFullscreenStatistic){
                statistic.put(type, 0.0);
            }
            for (JSONObject statistic : fbTimeNativeStatistic){
                statistic.put(type, 0.0);
            }
        }

        for (JSONObject json : data){
            String adType = json.getJSONObject("breakdowns").getString("placement");

            manageTimeDate(fbTimeSumStatistic, json, type);
            if (adType.equals(Banner)){
                manageTimeDate(fbTimeBannerStatistic, json, type);
            }else if (adType.equals(FullScreen)){
                manageTimeDate(fbTimeFullscreenStatistic, json, type);
            }else if (adType.equals(NativeAd)){
                manageTimeDate(fbTimeNativeStatistic, json, type);
            }
        }
    }

    //生成excel文件
    public static void creatExcel(String appName) {
        //增加填充率,点击率,千次展示收益
        makedate();
        //按国家统计数据
        List<JSONObject> fbCountryStatistic = new ArrayList<>();
        for (JSONObject json : fbCountryBannerStatistic){
            fbCountryStatistic.add(changeCountry(json));
        }
        for (JSONObject json : fbCountryFullscreenStatistic){
            fbCountryStatistic.add(changeCountry(json));
        }
        for (JSONObject json : fbCountryNativeStatistic){
            fbCountryStatistic.add(changeCountry(json));
        }
        //按时间统计数据
        List<JSONObject> fbTimeStatistic = new ArrayList<>();
        for (JSONObject json : fbTimeBannerStatistic){
            fbTimeStatistic.add(json);
        }
        for (JSONObject json : fbTimeFullscreenStatistic){
            fbTimeStatistic.add(json);
        }
        for (JSONObject json : fbTimeNativeStatistic){
            fbTimeStatistic.add(json);
        }

        //创建国家广告分类统计表
        if (Common.excelFileType.contains("1")){
            String[] countryHeaders = new String[]{"国家", "广告类型","请求数", "填充数", "展示次数", "点击次数", "填充率", "点击率", "千次展示收益", "预计收入"};
            try {
                ExcelWriter.createExcelFile("country_statistic", countryHeaders, fbCountryStatistic, appName);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        //创建时间广告分类统计表
        if (Common.excelFileType.contains("2")){
            String[] timeHeaders = new String[]{"时间", "广告类型", "请求数", "填充数", "展示次数", "点击次数", "填充率", "点击率", "千次展示收益", "预计收入"};
            try {
                ExcelWriter.createExcelFile("time_statistic", timeHeaders, fbTimeStatistic, appName);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        //创建时间所有广告统计表
        if (Common.excelFileType.contains("3")){
            String[] timeSumHeaders = new String[]{"时间", "广告类型", "请求数", "填充数", "展示次数", "点击次数", "填充率", "点击率", "千次展示收益", "预计收入"};
            try {
                ExcelWriter.createExcelFile("time_sum_statistic", timeSumHeaders, fbTimeSumStatistic, appName);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    //添加点击率,填充率和千次展示收益
    private static void makedate(){

        for (JSONObject json : fbCountryBannerStatistic){

            setJson(json);
            json.put("adType", "Banner");
        }
        for (JSONObject json : fbCountryFullscreenStatistic){

            setJson(json);
            json.put("adType", "Fullscreen");
        }
        for (JSONObject json : fbCountryNativeStatistic){

            setJson(json);
            json.put("adType", "Native");
        }
        for (JSONObject json : fbTimeSumStatistic){

            setJson(json);
            json.put("adType", "All");
        }
        for (JSONObject json : fbTimeBannerStatistic){

            setJson(json);
            json.put("adType", "Banner");
        }
        for (JSONObject json : fbTimeFullscreenStatistic){

            setJson(json);
            json.put("adType", "Fullscreen");
        }
        for (JSONObject json : fbTimeNativeStatistic){

            setJson(json);
            json.put("adType", "Native");
        }
    }

    private static JSONObject changeCountry(JSONObject json){

        for (JSONObject language : Common.regionJson){
            if (json.getString("country").equals(language.getString("code"))){
                json.put("country", language.getString("name"));
            }
        }
        return json;
    }

    private static void setJson(JSONObject json){
        if (json.get("request") != null && json.getDouble("fill")!= null&& json.getDouble("request") != 0){
            json.put("fillRate", json.getDouble("fill") / json.getDouble("request"));//填充率
        }
        if (json.getDouble("imp") != null && json.getDouble("click") != null && json.getDouble("imp") != 0){
            json.put("clickRate", json.getDouble("click") / json.getDouble("imp")); //点击率
        }
        if (json.getDouble("imp") != null && json.getDouble("revenue") != null && json.getDouble("imp") != 0){
            json.put("ecpm", json.getDouble("revenue") * 1000 / json.getDouble("imp"));//千次展示收益
        }
    }

    //根据国家处理数据
    private static void manageCountryDate(List<JSONObject> jsonObjects, JSONObject json, String type){

        String country = json.getJSONObject("breakdowns").getString("country");
        if (jsonObjects.size() == 0){
            JSONObject jsonObject2 = new JSONObject();
            jsonObject2.put("country", country);
            jsonObject2.put(type, Double.valueOf(json.getString("value")));
            jsonObjects.add(jsonObject2);
        }else {

            if (jsonObjects.get(0).getDouble(type) == null){
                for (JSONObject js : jsonObjects){
                    js.put(type, Double.valueOf(json.getString("value")));
                }
            }

            Boolean flag = true;
            for (JSONObject js : jsonObjects){
                if (country.equals(js.getString("country"))){
                    js.put(type, js.getDouble(type) + Double.valueOf(json.getString("value")));
                    flag = false;
                }
            }
            if (flag){
                if (!country.equals("REDACTED")){

                    JSONObject jsonObject2 = new JSONObject();
                    jsonObject2.put("country", country);
                    jsonObject2.put(type, Double.valueOf(json.getString("value")));
                    jsonObjects.add(jsonObject2);
                }
            }
        }
    }

    //根据时间处理数据
    private static void manageTimeDate(List<JSONObject> jsonObjects, JSONObject json, String type) {

        String time = json.getString("time").substring(0, 10);
        for (JSONObject timeStatistic : jsonObjects){
            if (timeStatistic.getString("time").equals(time)){
                timeStatistic.put(type, timeStatistic.getDoubleValue(type) + Double.valueOf(json.getString("value")));
            }
        }
    }

    //初始化数据
    public static void resetData(){
        fbTimeSumStatistic = new ArrayList<>();
        fbTimeBannerStatistic = new ArrayList<>();
        fbTimeFullscreenStatistic = new ArrayList<>();
        fbTimeNativeStatistic = new ArrayList<>();
        fbCountryBannerStatistic = new ArrayList<>();
        fbCountryFullscreenStatistic = new ArrayList<>();
        fbCountryNativeStatistic = new ArrayList<>();
    }
}
